package com.atguigu.minio;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

public class App {
    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String bucketName = "b002";

        //endpoint:端点，就是minio的URL
        MinioClient minioClient = MinioClient.builder()
                .endpoint("http://192.168.232.100:9000")
                .credentials("minioadmin","minioadmin").build();

        //判断桶是否存在
        boolean flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if(!flag){
            //创建桶
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            //设置桶访问策略
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
                    .bucket(bucketName)
                    .config("""
                            {
                                  "Statement" : [ {
                                    "Action" : "s3:GetObject",
                                    "Effect" : "Allow",
                                    "Principal" : "*",
                                    "Resource" : "arn:aws:s3:::b002/*"
                                  } ],
                                  "Version" : "2012-10-17"
                                }
                            """)
                    .build());
        }
        minioClient.uploadObject(UploadObjectArgs.builder()
                        .bucket(bucketName)
                        .object("hello.jpg")
                        .filename("D:\\atguigu2021\\2023课件\\05.后端工程化\\11.新项目一尚庭公寓\\资料\\图片资源\\7.images\\房间-卧室-2.jpg")
                .build());
    }
}
